// #include "app_ble.h"
// #include "drv_manager.h"
// #include <math.h>

// #define TAG "APP BLE"


// /*---------------- 1. 2^q 查表 ----------------*/
// static const uint16_t pow2_tab[] = {
//   0x0100,0x0110,0x0121,0x0133,0x0146,0x0159,0x016D,0x0182,
//   0x0197,0x01AD,0x01C4,0x01DC,0x01F5,0x020F,0x022A,0x0246,
//   0x0263,0x0281,0x02A0,0x02C0,0x02E1,0x0303,0x0326,0x034A,
//   0x036F,0x0395,0x03BC,0x03E4,0x040D,0x0437,0x0462,0x048E,
//   0x04BB,0x04E9,0x0518,0x0548,0x0579,0x05AB,0x05DE,0x0612,
//   0x0647,0x067D,0x06B4,0x06EC,0x0725,0x075F,0x079A,0x07D6,
//   0x0813,0x0851,0x0890,0x08D0,0x0911,0x0953,0x0996,0x09DA,
//   0x0A1F,0x0A65,0x0AAC,0x0AF4,0x0B3D,0x0B87,0x0BD2,0x0C1E,
//   0x0C6C,0x0CBA,0x0D0A,0x0D5B,0x0DAD,0x0E00,0x0E54,0x0EA9,
//   0x0F00,0x0F57,0x0FB0,0x1009,0x1064,0x10C0,0x111D,0x117C,
//   0x11DB,0x123C,0x129E,0x1301,0x1366,0x13CB,0x1432,0x149A,
//   0x1503,0x156E,0x15DA,0x1647,0x16B5,0x1725,0x1796,0x1808,
//   0x187C,0x18F0,0x1966,0x19DE,0x1A57,0x1AD1,0x1B4D,0x1BCA,
//   0x1C49,0x1CC9,0x1D4B,0x1DCE,0x1E53,0x1ED9,0x1F61,0x1FEA,
//   0x2075,0x2102,0x2190,0x2220,0x22B2,0x2345,0x23DA,0x2471,
//   0x2509,0x25A3,0x263F,0x26DD,0x277D,0x281F,0x28C3,0x2969,
//   0x2A11,0x2ABB,0x2B67,0x2C16,0x2CC6,0x2D79,0x2E2E,0x2EE5,
//   0x2F9E,0x305A,0x3117,0x31D7,0x3299,0x335E,0x3425,0x34EF,
//   0x35BB,0x368A,0x375B,0x382F,0x3905,0x39DE,0x3AB9,0x3B97,
//   0x3C77,0x3D5A,0x3E40,0x3F28,0x4013,0x4100,0x41F0,0x42E3,
//   0x43D9,0x44D2,0x45CD,0x46CB,0x47CC,0x48D0,0x49D7,0x4AE1,
//   0x4BED,0x4CFC,0x4E0E,0x4F23,0x503B,0x5156,0x5274,0x5395,
//   0x54B9,0x55E0,0x570A,0x5838,0x5969,0x5A9E,0x5BD6,0x5D12,
//   0x5E51,0x5F94,0x60DA,0x6224,0x6371,0x64C2,0x6616,0x676E,
//   0x68CA,0x6A29,0x6B8C,0x6CF2,0x6E5C,0x6FC9,0x713A,0x72AF,
//   0x7428,0x75A4,0x7725,0x78A9,0x7A32,0x7BBE,0x7D4E,0x7EE2,
//   0x807A,0x8216,0x83B7,0x855C,0x8705,0x88B2,0x8A64,0x8C1A,
//   0x8DD5,0x8F95,0x915A,0x9323,0x94F1,0x96C4,0x989C,0x9A79,
//   0x9C5B,0x9E42,0xA02E,0xA220,0xA417,0xA614,0xA817,0xAA20,
//   0xAC2F,0xAE44,0xB05F,0xB281,0xB4A9,0xB6D8,0xB90D,0xBB49,
//   0xBD8C,0xBFD6,0xC227,0xC47F,0xC6DF,0xC946,0xCBB4,0xCE2A,
//   0xD0A8,0xD32E,0xD5BD,0xD854,0xDAF4,0xDD9C,0xE04C,0xE304,
//   0xE5C5,0xE88F,0xEB62,0xEE3E,0xF123,0xF412,0xF70A,0xFA0C,
//   0xFD18
// };


// static uint16_t isqrt32(uint32_t a)   /* Q16.0 -> Q8.8 */
// {
//     uint16_t x = 0, s = 0x8000;
//     for (int i = 0; i < 16; ++i) {
//         uint32_t t = ((uint32_t)(x | s)) * (x | s);
//         if (t <= a) x |= s;
//         s >>= 1;
//     }
//     return x;                       /* 结果 8.8 定点 */
// }



// void trilaterate_car(const int8_t rssi[3], int16_t *x8, int16_t *y8)
// {
//     /* 常量：RSSI0 = -60 dBm； n = 2.5； 车宽 1.9 m -> Q8.8 为 486 */
//     const int16_t W_q8 = 486;                 /* 1.9 * 256 */
//     const int8_t  RSSI0 = -60;

//     /* 1. 计算 k = (RSSI0 - rssi) * 77 >> 8，范围 0…255 */
//     uint8_t k[3];
//     for (int i = 0; i < 3; ++i) {
//         int16_t t = (RSSI0 - rssi[i]) * 77;
//         k[i] = (uint8_t)((t >> 8) & 0xFF);    /* 相当于 /10n 的近似 */
//     }

//     /* 2. 查表得距离平方 (Q0.16) */
//     uint32_t d2[3];
//     for (int i = 0; i < 3; ++i) {
//         uint16_t d_q8 = pow2_tab[k[i]];       /* 距离 8.8 */
//         d2[i] = (uint32_t)d_q8 * d_q8;        /* 距离平方 16.16 */
//     }

//     /* 3. 计算 x = (d1² - d2²) / (2W)  (Q8.8) */
//     int32_t num = (int32_t)d2[0] - (int32_t)d2[1];
//     *x8 = (int16_t)(num / (2 * W_q8));

//     /* 4. 计算 y = sqrt(d3² - x²)  (Q8.8) */
//     int32_t xq = (int32_t)(*x8) * (*x8);      /* x² 16.16 */
//     int32_t diff = (int32_t)d2[2] - xq;       /* 必须 ≥0 */
//     if (diff < 0) diff = 0;
//     *y8 = isqrt32((uint32_t)diff);
// }


// void ble_scan_callback(uint8_t *addr, char *name, int8_t rssi)
// {
//     // static int8_t rssi_array[3] = {0};

//     // if(memcpy(addr, drv_public_get_datas()->eeprom_datas.ble_datas.white_list_mac[1], 6) == 0)
//     // {
//     //     rssi_array[0] = rssi;
//     // }
//     // else if(memcpy(addr, drv_public_get_datas()->eeprom_datas.ble_datas.white_list_mac[2], 6) == 0)
//     // {
//     //     rssi_array[1] = rssi;
//     // }
//     // else if(memcpy(addr, drv_public_get_datas()->eeprom_datas.ble_datas.white_list_mac[3], 6) == 0)
//     // {
//     //     rssi_array[2] = rssi;
//     // }

//     // int16_t x, y;
//     // trilaterate_car(rssi_array, &x, &y);
//     // esay_logd(TAG, "x = %d, y = %d", x, y);



//     // esay_log_set_log_en(ESAY_LOG_INFO, false);
//     if (name)
//         esay_logd(TAG, "RSSI %d : %02X:%02X:%02X:%02X:%02X:%02X %s", rssi, addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], name);
//     else
//         esay_logd(TAG, "RSSI %d : %02X:%02X:%02X:%02X:%02X:%02X", rssi, addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
// }

// void app_ble_install(void)
// {
//     drv_ble_register_scan_cb(ble_scan_callback);

//     // drv_ble_set_state(BLE_STATE_SCAN_FAST);
//     // drv_ble_add_white_list(mac);
// }
